package com.android.fragmentdemo.adapters
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import com.android.fragmentdemo.R
import com.android.fragmentdemo.databinding.RvItemLooperImgBinding
import com.android.fragmentdemo.glideoptions.GlideApp
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.hengfoxwolf.commonbase.base.adapter.BaseRvAdapter
import com.hengfoxwolf.commonbase.base.databeans.DataCase

class LooperImgItemAdapter(activity: Activity) : BaseRvAdapter<DataCase, RvItemLooperImgBinding>() {

    private val mActivity = activity

    override fun getLayoutId(): Int = R.layout.rv_item_looper_img

    override fun bindData(holder: ViewHolder, dataItem: DataCase, position: Int) {
        //holder.binding.tvNum.text = "$position"
        holder.binding.tvNum.text = "${dataItem.num}"

        //holder.binding.sivImg.setImageBitmap(dataItem)

        //holder.binding.sivImg.setImageResource(R.mipmap.ic_launcher_round)
        Glide
            .with(mActivity)
            .load(dataItem.url)
            //.override(80,80) //指定图像大小
            .format(DecodeFormat.PREFER_RGB_565) //降低分辨率
            .diskCacheStrategy(DiskCacheStrategy.ALL) //本身数据与本地存储
            .dontAnimate() //禁止动画
            .into(holder.binding.sivImg)

//        Glide.with(mActivity)
//            .asBitmap()
//            .load(dataItem.url)
//            .placeholder(R.mipmap.ic_launcher_round)
//            .into(object : CustomTarget<Bitmap>() {
//                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//                    holder.binding.sivImg.setImageBitmap(resource)
//                }
//
//                override fun onLoadCleared(placeholder: Drawable?) {
//                    placeholder?.let {
//                        holder.binding.sivImg.background = it
//                    }
//                }
//        })

//        GlideApp.with(mActivity)
//            //.asBitmap()//与RequestListener<R>类型进行绑定
//            .createBitmap()
//            .createDrawable()
//            //.asDrawable()
//            //.asGif()
//            //.asFile()
//            .load(dataItem.url)
//            .applyAvatar(144 * 3)
//            //淡入淡出处理
//            //.transition(DrawableTransitionOptions.withCrossFade())
//            .listener(drawableListener)
//            .into(holder.binding.sivImg)


        Log.d(TAG, "position:${position}")


        //设置一个与位置相关的tag
        //holder.imageView.setTag(position);

        //异步加载图片
        //loadImageAsync(holder.imageView, imageUrls.get(position));
    }

    /*
    * private void loadImageAsync(ImageView imageView, String url) {
        int position = (int) imageView.getTag();
        //使用Glide示例
        Glide.with(imageView.getContext())
             .load(url)
             .addListener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }
                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                   //检查tag是否匹配当前position
                   if ((int) imageView.getTag() == position) {
                       imageView.setImageDrawable(resource);
                   }
                   return false;
                }
         })
         .into(imageView);
        }
    * */

    override fun getItemId(position: Int) = position.toLong()

    override fun setHasStableIds(hasStableIds: Boolean) {
        super.setHasStableIds(true)
    }

    override fun listenerWithBinding(binding: RvItemLooperImgBinding) {
        super.listenerWithBinding(binding)
        Log.d(TAG,"super.listenerWithBinding(binding)")
    }

    companion object {
        const val TAG = "StudyLayoutManagerAdapter"
    }
}